package com.hoow.shakoow.components.dialog

import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.hoow.shakoow.R
import com.hoow.shakoow.base.BaseDialogAction
import com.hoow.shakoow.databinding.DialogMessageBinding

class MessageDialog : DialogFragment(), BaseDialogAction {

    companion object {
        //点击确定
        const val ACTION_OK = 1

        //点击关闭
        const val ACTION_CLOSE = 0

        fun showDialog(
            fragmentManager: FragmentManager,
            message: String,
            confirm: String = "OK",
            onClickListener: (Int) -> Unit = {},
        ) {
            val dialog = MessageDialog()
            dialog.mMessage = message
            dialog.mConfirm = confirm
            dialog.mOnActionListener = onClickListener
            dialog.showNow(fragmentManager, MessageDialog::class.java.simpleName)
        }
    }

    lateinit var mViewBinding: DialogMessageBinding

    private var mOnActionListener: (Int) -> Unit = {}
    private var mMessage: String? = null
    private var mConfirm: String? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mViewBinding = DialogMessageBinding.inflate(layoutInflater)

        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initSubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mViewBinding.textSubmitButtonTitle.text = mMessage
        mViewBinding.submitButtonConfirm.mViewBinding.textSubmitButton.text =
            mConfirm
    }

    override fun initSubviews() {
        try {
            mViewBinding.submitButtonConfirm.setOnClickListener {
                mOnActionListener.invoke(ACTION_OK)
                dismiss()
            }

            mViewBinding.imageMessageDialogClose.setOnClickListener {
                mOnActionListener.invoke(ACTION_CLOSE)
                dismiss()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showWithUITag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {

    }

    override fun setOnActionClickListener(listener: (Int) -> Unit) {

    }

}